﻿using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
using System.Xml;

using Rmlife.Common;
using Rmlife.Site;
using Rmlife.Config;
using Rmlife.Config.Provider;


namespace Rmlife.Web
{
    /// <summary>
    /// HttpModule类
    /// </summary>
    public class HttpModule : System.Web.IHttpModule
    {
        /// <summary>
        /// 实现接口的Init方法
        /// </summary>
        /// <param name="context"></param>
        public void Init(HttpApplication context)
        {
            context.Error += new EventHandler(Application_OnError);

            try
            {
                if (!RegistryUtils.CheckPath(CompanyProperties._RegPath))
                {
                    throw new Exception("未测试到注册表相应键值，请确认注册表中相关项目");
                }
                else
                {
                    OnlineUsers.ResetOnlineList();
                }
            }
            catch (Exception ex)
            {
                // 不处理
                WriteError(context, ex.Message);
            }
        }

        public void Application_OnError(Object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;
            /*
            context.Response.Write("<html><body style=\"font-size:14px;\">");
            context.Response.Write("网站错误,请与开发商联系:<br />");
            context.Response.Write("<textarea name=\"errormessage\" style=\"width:80%; height:200px; word-break:break-all\">");
            context.Response.Write(System.Web.HttpUtility.HtmlEncode(context.Server.GetLastError().ToString()));
            context.Response.Write("</textarea>");
            context.Response.Write("</body></html>");
            context.Response.End();
            */
            WriteError(application, context.Server.GetLastError().ToString());
        }

        public void WriteError(HttpApplication application, string err)
        {
            HttpContext context = application.Context;

            context.Response.Write("<html><head><title>发生错误</title></head><body style=\"font-size:14px;\">");
            context.Response.Write("网站遇到错误,请与开发商联系:<br /><br />");
            context.Response.Write("<textarea name=\"errormessage\" style=\"width:80%; height:200px; word-break:break-all;border:solid 1px black;\" onfocus=\"this.select();\">");
            context.Response.Write("问题：" + System.Web.HttpUtility.HtmlEncode(err));
            context.Response.Write("\r\n位置：" + context.Request.Url.ToString());
            context.Response.Write("\r\n时间：" + DateTime.Now.ToString());
            context.Response.Write("</textarea>");
            context.Response.Write("</body></html>");
            context.Response.End();
        }

        /// <summary>
        /// 实现接口的Dispose方法
        /// </summary>
        public void Dispose()
        {
        }
    }
}
